
'''
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

'''
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.
    
    To request the provided software under a different license you can contact us at
    support@xoronos.com
'''

#####################################################
##                   Introduction                  ##
#####################################################
## This code example shows how to compute the      ##
## symmetric signature from a file.                ##
#####################################################

#####################################################
##                   Pre-requisits                 ##
#####################################################
## The Xoron matrix decompressed data structure    ##
## needs to be mined before running this code      ##
## example. Some examples on how to mine Xoron     ##
## matrices can be found in                        ##
## generate_xrn_matrix_all_rnd.py or               ##
## generate_xrn_matrix_one_third.py                ##
##                                                 ##
## A monomial key. An example on how to generate   ##
## keys can be found in                            ##
## generate_keys_with_wrapper_functions.py         ##
#####################################################

import xrnlib256 as xrn
import lorem_ipsum as li

def xrn_generate_signature_symmetric_wrapper_example ( ) :

    ##########################
    ## Variable definitions ##
    ##########################

    # library run time settings

    settings = xrn.xrn_crypto_extra_settings_t() 

    # settings.rnd_str                      | A string used to influence randomness
    # settings.crc_bytes                    | Checksum used in encryption ( accepted values 1 2 4 8 )
    # settings.cnt_bytes                    | Counter used in encryption ( accepted values 1 2 4 8 )
    # settings.pair_bytes                   | Tag used in split-join algorithms ( accepted values 1 2 4 8 )
    # settings.state_size                   | Define data block ( accepted values 4 < state_size < 256 )
    #                                       | a block is bytes is ( settings.state_size * sizeof(xnum_t) )
    # settings.thread_enable                | Use of threads for the cross-traverse operation
    # settings.const_exec_time              | Constant execution time in the cross-traverse operation
    #
    # settings.encoding_settings.i_settings | Input encoding settings
    # settings.encoding_settings.o_settings | Output encoding settings
    #
    #                                 Encoding settings
    #
    # o/i_settings.mode                     | either: xrn_parameters, xrn_parameters_plus_password,
    #                                       | std_parameters, std_parameters_plus_password, no_encoding 
    #                                       | std_parameters : the compiled settings
    #                                       | xrn_parameters : the settings coming from a file
    #                                       | std_parameters_plus_password : compiled parameters with a password
    #                                       | xrn_parameters_plus_password : file parameters with a password
    # o/i_settings.encoding_parameters_fp   | The file pointer is the mode is xrn_parameters 
    #                                       | or xrn_parameters_plus_password
    # o/i_settings.password                 | The password string if the mode is std_parameters
    #                                       | or std_parameters_plus_password
    # o/i_settings.password_length          | The length of the password if the mode is std_parameters
    #                                       | or std_parameters_plus_password

    ##########################
    ## Initialize variables ##
    ##########################

    monomial_key_fp = xrn.fopen( "xmonomial_key.xf", "rb" )
    decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
    signature_fp = xrn.fopen( "signature.xf", "wb" )

    if ( ( decompressed_xrn_matrix_fp == None ) or ( monomial_key_fp == None )
         or ( signature_fp == None ) ) :
        return -1

    # Initialize default run time settings

    if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( monomial_key_fp )
        xrn.fclose( signature_fp )
        return -1

    ##########################
    ## Performing signature ##
    ##########################

    if ( xrn.XSUCCESS != xrn.xrn_make_signature_symmetric_bytes_wrapper( li.LOREM_IPSUM_ARRAY,   # input bytes
                                               len ( li.LOREM_IPSUM_STR ), # input bytes length
                                               monomial_key_fp, # Xoron monomial key file pointer
                                               decompressed_xrn_matrix_fp,    # Xoron matrix file pointer
                                               signature_fp,    # output signature file pointer
                                               settings # run time settings
          ) ) :
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( monomial_key_fp )
        xrn.fclose( signature_fp )
        return -1

    if ( ( 0 != xrn.fclose( monomial_key_fp ) ) 
         or ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) )
         or ( 0 != xrn.fclose( signature_fp ) ) ) :
        return -1

    # An alternative way to perform the same computation can be achieved using the xrn_make_signature_symmetric_bytes function
    # if ( XSUCCESS != xrn_make_signature_symmetric_bytes( li.LOREM_IPSUM_ARRAY
    #                                  len( li.LOREM_IPSUM_STR),
    #                                  monomial_key, # monomial_key is of the type ( xmonomial_key_t )
    #                                  xrn_matrix, # Xoron_matrix is of the type ( xrn_matrix_t * )
    #                                  signature, # the signature variable is of the type ( xsym_signature_t * )
    #                                  settings
    #      ) ) :

    return 0


def xrn_verify_signature_symmetric_wrapper_example ( ) :

    ##########################
    ## Variable definitions ##
    ##########################

    # library run time settings

    settings = xrn.xrn_crypto_extra_settings_t ( )

    # settings.rnd_str                      | A string used to influence randomness
    # settings.crc_bytes                    | Checksum used in encryption ( accepted values 1 2 4 8 )
    # settings.cnt_bytes                    | Counter used in encryption ( accepted values 1 2 4 8 )
    # settings.pair_bytes                   | Tag used in split-join algorithms ( accepted values 1 2 4 8 )
    # settings.state_size                   | Define data block ( accepted values 4 < state_size < 256 )
    #                                       | a block is bytes is ( settings.state_size * sizeof(xnum_t) )
    # settings.thread_enable                | Use of threads for the cross-traverse operation
    # settings.const_exec_time              | Constant execution time in the cross-traverse operation
    #
    # settings.encoding_settings.i_settings | Input encoding settings
    # settings.encoding_settings.o_settings | Output encoding settings
    #
    #                                 Encoding settings
    #
    # o/i_settings.mode                     | either: xrn_parameters, xrn_parameters_plus_password,
    #                                       | std_parameters, std_parameters_plus_password, no_encoding 
    #                                       | std_parameters : the compiled settings
    #                                       | xrn_parameters : the settings coming from a file
    #                                       | std_parameters_plus_password : compiled parameters with a password
    #                                       | xrn_parameters_plus_password : file parameters with a password
    # o/i_settings.encoding_parameters_fp   | The file pointer is the mode is xrn_parameters 
    #                                       | or xrn_parameters_plus_password
    # o/i_settings.password                 | The password string if the mode is std_parameters
    #                                       | or std_parameters_plus_password
    # o/i_settings.password_length          | The length of the password if the mode is std_parameters
    #                                       | or std_parameters_plus_password

    ##########################
    ## Initialize variables ##
    ##########################

    monomial_key_fp = xrn.fopen( "xmonomial_key.xf", "rb" )
    decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
    signature_fp = xrn.fopen( "signature.xf", "rb" )

    if ( ( decompressed_xrn_matrix_fp == None ) or ( monomial_key_fp == None )
         or ( signature_fp == None ) ) :
        return -1

    # Initialize default run time settings

    if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
        xrn.fclose( monomial_key_fp )
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( signature_fp )
        return -1

    ######################
    ## Verify signature ##
    ######################

    if ( xrn.XSUCCESS != xrn.xrn_check_signature_symmetric_bytes_wrapper( li.LOREM_IPSUM_ARRAY,   # input bytes
                                               len ( li.LOREM_IPSUM_STR ), # input bytes length
                                               monomial_key_fp, # Xoron monomial key file pointer
                                               decompressed_xrn_matrix_fp,    # Xoron matrix file pointer
                                               signature_fp,    # input signature file pointer
                                               settings # run time settings
          ) ) :

        xrn.fclose( monomial_key_fp )
        xrn.fclose( decompressed_xrn_matrix_fp )
        xrn.fclose( signature_fp )
        return -1

    if ( ( 0 != xrn.fclose( monomial_key_fp ) ) 
         or ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) )
         or ( 0 != xrn.fclose( signature_fp ) ) ) :
        return -1

    # An alternative way to perform the same computation can be achieved using the xrn_make_signature_symmetric function
    # if ( xrn.XSUCCESS != xrn.xrn_check_signature_symmetric_bytes( li.LOREM_IPSUM_ARRAY,
    #                                   len ( li.LOREM_IPSUM_STR ),
    #                                   monomial_key, # monomial_key is of the type ( xmonomial_key_t )
    #                                   xrn_matrix, # Xoron_matrix is of the type ( xrn_matrix_t * )
    #                                   signature, # the signature variable should be of the type ( xsym_signature_t )
    #                                   settings 
    #      )):

    return 0


#############
## Logging ##
#############

# Set the default logging file streams
# errors -> stderr
# warnings -> stdout
# notifications -> stdout
# debug -> stdout

xrn.xrn_set_default_log(  )

# To change the default logging you can use the functions below,
# where fp is the opened file pointer
# 
# xrn_change_error_log( fp )
# xrn_change_warning_log( fp )
# xrn_change_notification_log( fp )
# xrn_change_debug_log( fp )

print( "signature... " )
if ( 0 != xrn_generate_signature_symmetric_wrapper_example(  ) ) :
    print( "error in encryption " )
    exit( -1 )

print( "verify signature... " )
if ( 0 != xrn_verify_signature_symmetric_wrapper_example(  ) ) :
    print( "signature does not match" )
    exit( -1 )
else :
    print( "signature does match" )

exit( 0 )



